package org.jeecg.modules.farming.news.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.farming.news.entity.NewsComment;
import org.jeecg.modules.farming.news.entity.NewsCommentEva;
import org.jeecg.modules.farming.news.entity.NewsCommentPraise;
import org.jeecg.modules.farming.news.entity.VisitUser;
import org.jeecg.modules.farming.news.service.INewsCommentEvaService;
import org.jeecg.modules.farming.news.service.INewsCommentPraiseService;
import org.jeecg.modules.farming.news.service.INewsCommentService;
import org.jeecg.modules.farming.news.service.IVisitUserService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
* @Description: 新闻咨询-评价表
* @Author: jeecg-boot
* @Date:   2020-12-14
* @Version: V1.0
*/
@Api(tags="新闻咨询-评价表")
@RestController
@RequestMapping("/min/newsComment")
@Slf4j
public class MinNewsCommentController extends JeecgController<NewsComment, INewsCommentService> {
   @Autowired
   private INewsCommentService newsCommentService;
    @Autowired
    private INewsCommentEvaService evaService;
    @Autowired
    private INewsCommentPraiseService praiseService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IVisitUserService visitUserService;
   /**
    * 分页列表查询
    *
    * @param newsComment
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   @AutoLog(value = "新闻咨询-评价表-分页列表查询")
   @ApiOperation(value="新闻咨询-评价表-分页列表查询", notes="新闻咨询-评价表-分页列表查询")
   @GetMapping(value = "/list")
   public Result<?> queryPageList(NewsComment newsComment,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  HttpServletRequest req) {
       QueryWrapper<NewsComment> queryWrapper = QueryGenerator.initQueryWrapper(newsComment, req.getParameterMap());
       Page<NewsComment> page = new Page<NewsComment>(pageNo, pageSize);
       IPage<NewsComment> pageList = newsCommentService.page(page, queryWrapper);
       List<NewsComment> records = pageList.getRecords();
       if(records !=null && records.size()>0){
           for(int i =0;i<records.size();i++){
               NewsComment comment = records.get(i);
               //设置是否点赞
               NewsCommentPraise praiseServiceOne = praiseService.getOneByOpenIdAndCommentId(newsComment.getLoginUserId(),comment.getId());
               if(praiseServiceOne !=null){
                   records.get(i).setIsPraise(1);
               }

               //设置评论内容再评论
               NewsCommentEva eva = new NewsCommentEva();
               eva.setNewsComnentId(comment.getId());
               //eva.setUserId(newsComment.getLoginUserId());
               QueryWrapper<NewsCommentEva> evaWrapper = new QueryWrapper<NewsCommentEva>(eva);
               List<NewsCommentEva> list = evaService.list(evaWrapper);
               records.get(i).setEvaList(list);
               records.get(i).setReCount(list !=null ? list.size() : 0);
           }
           pageList.setRecords(records);
       }
       return Result.OK(pageList);
   }

   /**
    *   添加
    *
    * @param newsComment
    * @return
    */
   @AutoLog(value = "新闻咨询-评价表-添加")
   @ApiOperation(value="新闻咨询-评价表-添加", notes="新闻咨询-评价表-添加")
   @PostMapping(value = "/add")
   public Result<?> add(@RequestBody NewsComment newsComment) {
       LambdaQueryWrapper<VisitUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
       lambdaQueryWrapper.eq(VisitUser::getOpenId,newsComment.getOpenId());
       VisitUser user = visitUserService.getOne(lambdaQueryWrapper);
       if(user ==null){
           LambdaQueryWrapper<SysUser> sysUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
           sysUserLambdaQueryWrapper.eq(SysUser::getOpenId,newsComment.getOpenId());
           SysUser u = userService.getOne(sysUserLambdaQueryWrapper);
           newsComment.setUserName(u.getRealname());
           newsComment.setHeadImgUrl(u.getAvatar());
       }else{
           newsComment.setUserName(user.getNickName());
           newsComment.setHeadImgUrl(user.getHeadImgUrl());
       }
       newsComment.setUserName(user.getNickName());
       newsComment.setHeadImgUrl(user.getHeadImgUrl());
       newsComment.setIsPraise(0);
       newsComment.setPraiseCount(0);
       newsCommentService.save(newsComment);
       return Result.OK("添加成功！");
   }

   /**
    *  编辑
    *
    * @param newsComment
    * @return
    */
   @AutoLog(value = "新闻咨询-评价表-编辑")
   @ApiOperation(value="新闻咨询-评价表-编辑", notes="新闻咨询-评价表-编辑")
   @PutMapping(value = "/edit")
   public Result<?> edit(@RequestBody NewsComment newsComment) {
       newsCommentService.updateById(newsComment);
       return Result.OK("编辑成功!");
   }

   /**
    *   通过id删除
    *
    * @param id
    * @return
    */
   @AutoLog(value = "新闻咨询-评价表-通过id删除")
   @ApiOperation(value="新闻咨询-评价表-通过id删除", notes="新闻咨询-评价表-通过id删除")
   @DeleteMapping(value = "/delete")
   public Result<?> delete(@RequestParam(name="id",required=true) String id) {
       newsCommentService.removeById(id);
       return Result.OK("删除成功!");
   }

   /**
    *  批量删除
    *
    * @param ids
    * @return
    */
   @AutoLog(value = "新闻咨询-评价表-批量删除")
   @ApiOperation(value="新闻咨询-评价表-批量删除", notes="新闻咨询-评价表-批量删除")
   @DeleteMapping(value = "/deleteBatch")
   public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
       this.newsCommentService.removeByIds(Arrays.asList(ids.split(",")));
       return Result.OK("批量删除成功!");
   }

   /**
    * 通过id查询
    *
    * @param id
    * @return
    */
   @AutoLog(value = "新闻咨询-评价表-通过id查询")
   @ApiOperation(value="新闻咨询-评价表-通过id查询", notes="新闻咨询-评价表-通过id查询")
   @GetMapping(value = "/queryById")
   public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
       NewsComment newsComment = newsCommentService.getById(id);
       //设置评论内容再评论
       NewsCommentEva eva = new NewsCommentEva();
       eva.setNewsComnentId(newsComment.getId());
       eva.setOpenId(newsComment.getOpenId());
       QueryWrapper<NewsCommentEva> evaWrapper = new QueryWrapper<NewsCommentEva>(eva);
       List<NewsCommentEva> list = evaService.list(evaWrapper);
       newsComment.setEvaList(list);

       if(newsComment==null) {
           return Result.error("未找到对应数据");
       }
       return Result.OK(newsComment);
   }

   /**
   * 导出excel
   *
   * @param request
   * @param newsComment
   */
   @RequestMapping(value = "/exportXls")
   public ModelAndView exportXls(HttpServletRequest request, NewsComment newsComment) {
       return super.exportXls(request, newsComment, NewsComment.class, "新闻咨询-评价表");
   }

   /**
     * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
   @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
   public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
       return super.importExcel(request, response, NewsComment.class);
   }

}
